package com.czl.module_base.adapter;

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @author xubin
 * @date on 2019/5/21  18:11
 */
public abstract class ListTreeAdapter<VH extends ListTreeAdapter.ListTreeViewHolder>
        extends RecyclerView.Adapter<VH> {

    protected ListTree tree;
    protected float indentation;

    public ListTreeAdapter() {
    }

    public ListTreeAdapter(ListTree tree) {
        this.tree = tree;
    }

    public ListTreeAdapter(ListTree tree, float indentation) {
        this.tree = tree;
        this.indentation = indentation;
    }

    public ListTree getTree() {
        return tree;
    }

    @NonNull
    @Override
    final public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //子类创建自己的row view
        return onCreateNodeView(parent, viewType);
    }

    protected abstract @NotNull
    VH onCreateNodeView(ViewGroup parent, int viewType);

    protected abstract void onBindNodeViewHolder(VH viewHolder, int position);

    protected void onBindNodeViewHolder(VH viewHolder, int position, @NonNull List<Object> payloads) {

    }

    @Override
    final public int getItemViewType(int position) {
        ListTree.TreeNode node = tree.getNodeByPlaneIndex(position);
        return node.getLayoutResId();
    }

    @Override
    final public void onBindViewHolder(@NonNull VH holder, int position) {
        onBindNodeViewHolder(holder, position);
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position, @NonNull List<Object> payloads) {
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
        } else {
            onBindNodeViewHolder(holder, position, payloads);
        }
    }

    @Override
    final public int getItemCount() {
        return tree.size();
    }

    public void notifyTreeItemInserted(ListTree.TreeNode parent, ListTree.TreeNode node) {
        int parentPlaneIndex = tree.getNodePlaneIndex(parent);
        if (parent.isExpand() && node != null) {
            //已展开
            super.notifyItemInserted(tree.getNodePlaneIndex(node));
        } else {
            //未展开，需展开爸爸
            int count = tree.expandNode(parentPlaneIndex);
            //通知改变爸爸的状态
            super.notifyItemChanged(parentPlaneIndex);
            //通知更新展开的子孙行们
            notifyItemRangeInserted(parentPlaneIndex + 1, parent.getDescendantCount());
        }
    }

    public void notifyTreeItemRemoved(ListTree.TreeNode node) {
        if (node != null) {
            int nodePlaneIndex = tree.getNodePlaneIndex(node);
            tree.removeNode(node);
            super.notifyItemRemoved(nodePlaneIndex);
            super.notifyItemRangeChanged(nodePlaneIndex, tree.size() - nodePlaneIndex);
        }
    }

    public class ListTreeViewHolder extends RecyclerView.ViewHolder {
        protected ViewGroup containerView;

        public ListTreeViewHolder(View itemView) {
            super(itemView);
        }

        protected void changeNode() {
            int planePos = getAbsoluteAdapterPosition();
            ListTree.TreeNode node = tree.getNodeByPlaneIndex(planePos);
            if (node.isShowExpandIcon()) {
                int nodePlaneIndex = tree.getNodePlaneIndex(node);
                if (node.isExpand()) {
                    //收起
                    int count = tree.collapseNode(nodePlaneIndex);
                    notifyItemChanged(nodePlaneIndex);
                    //通知view将相关的行删掉
                    notifyItemRangeRemoved(nodePlaneIndex + 1, count);
                } else {
                    //展开
                    int count = tree.expandNode(nodePlaneIndex);
                    notifyItemChanged(nodePlaneIndex);
                    //通知view插入相关的行
                    notifyItemRangeInserted(nodePlaneIndex + 1, count);
                }
            }
        }
    }

    public interface OnItemClickListener<T> {
        /**
         * 点击
         *
         * @param t
         * @param title
         * @param pIdPath
         */
        void onItemClick(T t, String title, String pIdPath);
    }
}
